package com.shuqi.interfaces.web;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.text.TextUtils;

import com.shuqi.activity.BookCoverActivity;
import com.shuqi.activity.LoginActivity;
import com.shuqi.activity.WebCommonActivity;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.Config;
import com.shuqi.common.ConfigVersion;
import com.shuqi.common.utils.JsonUtils;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.ToastUtil;
import com.shuqi.database.model.UserInfo;
import com.shuqi.model.manager.MyAccountManager;

/**
 * 网页版书城javascript接口，需写一个内部类 继承此接口实现需要实现的功能
 * @author yanghuawei
 */
public abstract class ShuqiWebJsBaseInterface {
    private final String logTag = "ShuqiWebJsBaseInterface";
    public static final String JS_OBJECT = "anShuqiForSqWebJS";
    public static final int JUMPLOGIN_REQUESTCODE = 100;
    
    /**JS 网页交互的返回值：执行成功*/
    public static final int JSACTION_RETURN_SUCCESS = 1;
    /**JS 网页交互的返回值：执行失败*/
    public static final int JSACTION_RETURN_FAIL = 0;

    public abstract void refresh();
    public abstract Activity getActivity();
    public abstract void loadError(String jsonData);//加载错误时调用 
//    /**控制loading的显隐：true显示/false隐藏*/
//    public abstract void controlLoading(boolean isShowLoading);
    
    /** 网页打开客户端封面页
     * @param "bookId" 书籍ID
     */
    public int openAppBookCover(String jsonData){
        Log4an.i(logTag, "openAppBookCover() "+jsonData);
        if(!TextUtils.isEmpty(jsonData)){
            try {
                JSONObject jsonObject = new JSONObject(jsonData);
                final String bookId = JsonUtils.getStringFromJsonObj(jsonObject, "bookId");
                if(!TextUtils.isEmpty(bookId)){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(getActivity() instanceof WebCommonActivity){
                                BookCoverActivity.openBookCoverForAvoidCycle(getActivity(), bookId);
                            }else{
                                BookCoverActivity.openBookCover(getActivity(), bookId);
                            }
                        }
                    });
                    return JSACTION_RETURN_SUCCESS;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else{
            ToastUtil.showMessage("参数空异常");
        }
        return JSACTION_RETURN_FAIL;
    }
    
    /** 打开客户端二级网页页面
     * @param "webTitle" 新页面的title
     * @param "webUrl" 网页地址
     */
    public int openAppWebPage(String jsonData){
        Log4an.i(logTag, "openAppWebPage() "+jsonData);
        if(!TextUtils.isEmpty(jsonData)){
            try {
                JSONObject jsonObject = new JSONObject(jsonData);
                final String webTitle = JsonUtils.getStringFromJsonObj(jsonObject, "webTitle");
                final String webUrl = JsonUtils.getStringFromJsonObj(jsonObject, "webUrl");
                Log4an.i(logTag, "webTitle="+webTitle+",weburl="+webUrl);
                if(!TextUtils.isEmpty(webUrl)){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(getActivity() instanceof WebCommonActivity){
                                WebCommonActivity.openWebCommonForAvoidCycle(getActivity(), webTitle, webUrl);
                            }else{
                                WebCommonActivity.openWebCommon(getActivity(), webTitle, webUrl);
                            }
                        }
                    });
                    return JSACTION_RETURN_SUCCESS;
                }else{
                    ToastUtil.showMessage("链接为空");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else{
            ToastUtil.showMessage("参数空异常");
        }
        return JSACTION_RETURN_FAIL;
    };
    
    /**
     * 获取用户基本信息
     * @param jsonData
     * @return
     */
    public String getAppUserInfo(String jsonData){
        Log4an.i(logTag, "getUserInfo() "+jsonData);
        boolean isAvailable = MyAccountManager.isAvailableUser(ShuqiApplication.getContext());
        if(isAvailable){
            UserInfo userInfo = MyAccountManager.getInstance(ShuqiApplication.getContext());
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("user_id", JsonUtils.encodeString(userInfo.getUserId()));
                jsonObject.put("nickName", JsonUtils.encodeString(userInfo.getNickName()));
                jsonObject.put("session", JsonUtils.encodeString(userInfo.getSession()));
                String result = jsonObject.toString(); 
                Log4an.i(logTag, result);
                return result;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return "{}";
    }
    /** 网页打开客户端登录页
     * @param "bookId" 书籍ID
     */
    public int openAppLoginActivity(String jsonData){
        Log4an.i(logTag, "openAppLoginActivity() "+jsonData);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                LoginActivity.openVerifyLoginForResultAndBack(getActivity(),JUMPLOGIN_REQUESTCODE);
            }
        });
        return JSACTION_RETURN_SUCCESS;
    }
    
    /***
     * 获取客户端基本参数
     * @param jsonData
     * @return
     */
    public String getAppConfigVersion(String jsonData){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("soft_id", JsonUtils.encodeString(Config.SOFT_ID));
            jsonObject.put("ver", JsonUtils.encodeString(Config.VERSION_INFO));
            jsonObject.put("appVer", JsonUtils.encodeString(ConfigVersion.mAppVerName));
            jsonObject.put("placeid", JsonUtils.encodeString(ConfigVersion.ANSITEID));
            jsonObject.put("imei", JsonUtils.encodeString(ConfigVersion.IMEI));
            jsonObject.put("sn", JsonUtils.encodeString(ConfigVersion.SN));
            String result = jsonObject.toString(); 
            Log4an.i(logTag, result);
            return result;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "{}";
    }
    
    /**显示提示语*/
    public int showAppMessage(String jsonData){
        Log4an.i(logTag, "showAppMessage() "+jsonData);
        if(!TextUtils.isEmpty(jsonData)){
            try {
                JSONObject jsonObject = new JSONObject(jsonData);
                String message = JsonUtils.getStringFromJsonObj(jsonObject, "message");
                if(!TextUtils.isEmpty(message)){
                    ToastUtil.showMessage(message);
                    return JSACTION_RETURN_SUCCESS;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else{
            ToastUtil.showMessage("参数空异常");
        }
        return JSACTION_RETURN_FAIL;
    }
    
//    /***
//     * 控制客户端loading页面的显隐
//     * @param jsonData
//     * @return
//     */
//    public int controlAppPageActive(String jsonData){
//        Log4an.i(logTag, "controlAppLoading() "+jsonData);
//        if(!TextUtils.isEmpty(jsonData)){
//            try {
//                JSONObject jsonObject = new JSONObject(jsonData);
//                String loading = JsonUtils.getStringFromJsonObj(jsonObject, "loading");
////                controlLoading("show".equals(loading));
//                return JSACTION_RETURN_SUCCESS;
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//        }else{
//            ToastUtil.showMessage("参数空异常");
//        }
//        return JSACTION_RETURN_FAIL;
//    }

    
//    /** 判断当前用户的可用性/验证状态
//     * @return 1:已验证，0:未验证
//     */
//    public int checkUserAvailable(String jsonData){
//        boolean isAvailable = MyAccountManager.isAvailableUser(ShuqiApplication.getContext());
//        if(!isAvailable){
//            CheckUserListener listener = new CheckUserListener() {
//                @Override
//                public void onPassed() {
//                    refresh();
//                }
//            };
//            CheckUser.checkUserStateBeforeJump(getActivity(), listener);
//        }
//        return isAvailable?JSACTION_RETURN_SUCCESS:JSACTION_RETURN_FAIL;
//    }

}
